package com.suixun.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suixun.dao.MessageHistoryManageDAO;
import com.suixun.dao.MessageHistoryMapper;
import com.suixun.dao.sendmessagemanage.SendMessageManageDAO;
import com.suixun.entity.SendMessageInfo;
import com.suixun.service.MessageHistoryService;
@Service
public class MessageHistoryServiceImpl implements MessageHistoryService {

	@Resource
	private MessageHistoryMapper messageHistoryMapper;
	@Resource
	private SendMessageManageDAO sendMessageManageDAO;
	@Resource
	private MessageHistoryManageDAO messageHistoryManageDAO;
	
	@Override
	@Transactional
	public void addMessage(SendMessageInfo sendMessage) {
		 //修改消息推送次数
		 Map<String,Object> map=new HashMap<>();
		 int count=sendMessage.getSendcount()-1;
		 map.put("sendcount", count);
		 map.put("messageid", sendMessage.getMessageid());
		 if(count==0){
			 map.put("status", 0);
		 }
		 sendMessageManageDAO.updateSendMessage(map);
		 
		 //保存消息记录
		 //保存发送全部用户的信息
		 map = new HashMap<>();
		 map.put("operationername", sendMessage.getOperationername());
		 map.put("title", sendMessage.getTitle());
		 map.put("content", sendMessage.getContent());
		 if (sendMessage.getReceivetype().equals("1")) {
			 map.put("sendType", 1);
		 }else {
			 List<String> telephones = new ArrayList<>();
			 String[] telephoneArr = sendMessage.getPeopletels().split("[,，]");
			 for(String telStr : telephoneArr){
				 telephones.add(telStr);
			 }
			 map.put("telephoneList", telephones);
		 }
		 messageHistoryManageDAO.addMessageHistory(map);
	}

}
